package com.sxg.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxg.convert.ConvertUtils;
import com.sxg.convert.SxgCategoryConvert;
import com.sxg.dao.SxgCategoryDao;
import com.sxg.entity.SxgCategoryEntity;
import com.sxg.query.SxgCategoryQuery;
import com.sxg.service.SxgCategoryService;
import com.sxg.vo.SxgCategoryVO;
import com.sxg.vo.resp.CategoryRespVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sxg.page.PageResult;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类
 *
 * @author 浪漫写尽歌谱 3298751420@qq.com
 * @since 1.0.0 2024-03-07
 */
@Service
@AllArgsConstructor
public class SxgCategoryServiceImpl extends ServiceImpl<SxgCategoryDao, SxgCategoryEntity> implements SxgCategoryService {

    @Override
    public PageResult<SxgCategoryVO> page(SxgCategoryQuery query) {
        IPage<SxgCategoryEntity> page = baseMapper.selectPage(PageResult.getPage(query), getWrapper(query));

        return new PageResult<>(SxgCategoryConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<SxgCategoryEntity> getWrapper(SxgCategoryQuery query) {
        LambdaQueryWrapper<SxgCategoryEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(SxgCategoryVO vo) {
        SxgCategoryEntity entity = SxgCategoryConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SxgCategoryVO vo) {
        SxgCategoryEntity entity = SxgCategoryConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public List<CategoryRespVo> selectList() {
        List<SxgCategoryEntity> sxgCategoryEntities = baseMapper.selectList(new LambdaQueryWrapper<>());

        //查询层次查出子数据
        List<CategoryRespVo> respVoList = sxgCategoryEntities.parallelStream()
                .filter(sxgCategoryEntity -> sxgCategoryEntity.getParentCid() == 0)
                .map(sxgCategoryEntity -> {
                    CategoryRespVo categoryRespVo = ConvertUtils.toCategoryRespVo(sxgCategoryEntity);
                    List<CategoryRespVo> categoryChild = getCategoryChild(sxgCategoryEntities, sxgCategoryEntity.getId());
                    if (!categoryChild.isEmpty()) {
                        categoryRespVo.setCategoryChild(categoryChild);
                    }
                    return categoryRespVo;
                }).filter(categoryRespVo->categoryRespVo.getCategoryChild() != null)
                .collect(Collectors.toList());

        for (CategoryRespVo categoryRespVo : respVoList) {
            ArrayList<CategoryRespVo> arrayList = new ArrayList<>();
            for (CategoryRespVo respVo : categoryRespVo.getCategoryChild()) {
                List<CategoryRespVo> categoryChild = respVo.getCategoryChild();
                if (categoryChild != null) {
                    categoryChild.parallelStream()
                            .filter(categoryRespVo1-> categoryRespVo1.getIcon() != null)
                            .forEach(arrayList::add);
                }
            }
            categoryRespVo.setCategoryChild(arrayList);
        }



        return respVoList.parallelStream()
                .filter(categoryRespVo -> categoryRespVo.getCategoryChild()!= null&&!categoryRespVo.getCategoryChild().isEmpty())
                .collect(Collectors.toList());
    }

    private List<CategoryRespVo> getCategoryChild(List<SxgCategoryEntity> categoryList, Long parentId) {

        return categoryList.parallelStream()
                .filter(sxgCategoryEntity -> sxgCategoryEntity.getParentCid().equals(parentId))
                .map(sxgCategoryEntity -> {
                    CategoryRespVo categoryRespVo = ConvertUtils.toCategoryRespVo(sxgCategoryEntity);
                    List<CategoryRespVo> categoryChild = getCategoryChild(categoryList, sxgCategoryEntity.getId());
                    if (!categoryChild.isEmpty()) {
                        categoryRespVo.setCategoryChild(categoryChild);
                    }
                    return categoryRespVo;
                })
                .collect(Collectors.toList());
    }

}